package main.smarthouse.beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import smarthouse.dto.HouseDTO;
import smarthouse.dto.UserDTO;
import smarthouse.ejb.ElectronicDAORemote;
import smarthouse.ejb.HouseDAORemote;
import smarthouse.ejb.UserDAORemote;

/**
 * @author Sergiu
 *
 */
@SessionScoped
@ManagedBean(name = "userBean")
public class UserBean {

	@EJB
	UserDAORemote userDAO;

	@EJB
	HouseDAORemote houseDAO;

	@EJB
	ElectronicDAORemote electronicDAO;

	private String username;
	private String password;

	@SuppressWarnings("unused")
	private UserDTO loggedUser;

	private boolean loggedIn = false;
	private int selectedHouse;
	private List<HouseDTO> houseList;

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public boolean getLoggedIn() {
		return loggedIn;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public int getSelectedHouse() {
		return selectedHouse;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setSelectedHouse(int selectedHouse) {
		this.selectedHouse = selectedHouse;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public List<HouseDTO> getHouseList() {
		return houseList;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setHouseList(List<HouseDTO> houseList) {
		this.houseList = houseList;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public UserDTO getLoggedUser() {
		return (UserDTO) FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("loggedUser");
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setLoggedUser(UserDTO loggedUser) {
		this.loggedUser = loggedUser;
	}

	/**
	 * Login
	 */
	public void login() {
		UserDTO loggedUser = userDAO.login(username, password);

		if (loggedUser == null) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(
							"Login failed: Incorrect username/password"));

		} else {
			loggedIn = true;
			FacesContext.getCurrentInstance().getExternalContext()
					.getSessionMap().put("loggedUser", loggedUser);
			houseList = new ArrayList<>();
			houseList.add(houseDAO.getHousebyId(loggedUser.getHouse()));
		}
	}

	/**
	 * Login continue
	 * 
	 * @return
	 */
	public String loginCont() {
		if (selectedHouse != 0 || houseList.isEmpty()) {
			FacesContext.getCurrentInstance().getExternalContext()
					.getSessionMap()
					.put("selectedHouse", houseDAO.getHousebyId(selectedHouse));
			return "userPage.xhtml?faces-redirect=true";
		} else
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage("Select a house"));
		return null;
	}

	/**
	 * Reset the login
	 */
	public void reset() {
		username = "";
		password = "";
		loggedIn = false;
	}

	/**
	 * Shut down house electronics
	 * 
	 * @return
	 */
	public String houseShutdown() {

		electronicDAO.houseShutdown();

		return logout();
	}

	/**
	 * Log out
	 * 
	 * @return
	 */
	public String logout() {
		FacesContext.getCurrentInstance().getExternalContext()
				.invalidateSession();
		return "index.xhtml?faces-redirect=true";
	}
}
